package com.culturalCenter.dataCenter.service.impl.training;

import com.culturalCenter.dataCenter.customEntity.responseParam.training.TrainingAudienceAnalysisResponse;
import com.culturalCenter.dataCenter.customEntity.training.CustomAge;
import com.culturalCenter.dataCenter.customEntity.training.CustomTrainingInfoStatistics;
import com.culturalCenter.dataCenter.mapper.training.TrainingApplyMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingAudienceCountMapper;
import com.culturalCenter.dataCenter.service.training.TrainingAudienceService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author : weicai
 * @date : 15:57 2020/10/12
 */
@Service
public class TrainingAudienceServiceImpl implements TrainingAudienceService {

    @Resource
    private TrainingAudienceCountMapper trainingAudienceCountMapper;
    @Resource
    private TrainingApplyMapper trainingApplyMapper;

    @Override
    public TrainingAudienceAnalysisResponse getAudienceAnalysisData(String trainingId) {

        TrainingAudienceAnalysisResponse trainingAudienceAnalysisResponse = new TrainingAudienceAnalysisResponse();
        //性别统计
        List<CustomTrainingInfoStatistics> genderList = trainingAudienceCountMapper.queryGenderByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(genderList)) {
            genderList.forEach(gender -> {
                boolean isMale = gender.getMaleNum() > gender.getFemaleNum();
                gender.setMostGender(isMale ? "男" : "女");
                gender.setMostGenderRate(isMale ? gender.getMaleRate() : gender.getFemaleRate());
            });
            trainingAudienceAnalysisResponse.setGenderList(genderList);
        }
        //年龄统计
        List<CustomAge> ageIntervalList = trainingApplyMapper.queryAgeByTrainingId(trainingId);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (CollectionUtils.isNotEmpty(ageIntervalList)) {
            int sum = ageIntervalList.stream().mapToInt(CustomAge::getSignUpNum).sum();
            for (CustomAge customAge : ageIntervalList) {
                customAge.setMostGenerationRate(sum == 0 ? "0.00" : decimalFormat.format((float) customAge.getSignUpNum() / sum * 100));
            }
            trainingAudienceAnalysisResponse.setAgeIntervalList(ageIntervalList);
        }
        return trainingAudienceAnalysisResponse;
    }

}
